using Godot;
using System;

public partial class BTNode : Node//根节点，所有行为树节点的模板
{
	[Signal]
	public delegate void completedEventHandler(bool result);
	public Characters _character;
	public Sight _sight;
    public override void _Ready()
    {
        _sight = _character.GetNode<Sight>("Sight");
    }
    public enum BTSate{//节点状态
		SUCCESS,
		FAILE,
		RUNNING,
		CANCELED
	}
	public BTSate _state = BTSate.CANCELED;
	//节点函数
	public virtual bool succeed()
	{
		_state = BTSate.SUCCESS;
		return true;
	}

	public virtual bool faile()
	{
		_state = BTSate.FAILE;
		return false;
	}

	public virtual void run()
	{
		_state = BTSate.RUNNING;
	}

	public virtual void cancel()
	{
		_state = BTSate.CANCELED;
	}
	
	public Characters FindParentOfType<T>(Node node) where T : Node  //获取父节点
    {  
        Node parent = node.GetParent<Node>();  
        while (parent != null)  
        {  
            if (parent is Characters typedParent)  
            {  
                // 找到了特定类型的父节点  
                return typedParent;  
            }  
            // 继续向上遍历父节点  
            parent = parent.GetParent<Node>();  
        }  
        // 没有找到特定类型的父节点  
        return null;  
    }  

	public virtual double Utility()
	{
		return 0.0;
	}

	public bool wait(double sec)
	{
		var frame = sec * Engine.GetFramesPerSecond();
		if(Engine.GetProcessFrames()%frame == 0)
		{
			return true;
		}
		else
		{return false;}
	}
}
